<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/9/10
 * Time: 8:53
 */

namespace app\common\controller;


use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\Paylog;
use think\Controller;

class UserBonusFee extends Controller
{

    /* 增加积分记录
     * mid  top_mid card_id c_id us_id openid code orderid oid bonus mode type cate source remark
     *
     *
     * mid  top_mid card_id c_id us_id openid code orderid oid bonus mode type reason source  record_bonus
     */
    public function rec_user_bonus($param,$type){
        $ActionLogs=new ActionLogs();

        try {
            $ActionLogs->writelog("osapi","clientlogs","rec_user_bonus","1:",$param);
            $data = $param;
            $data['num'] = $param['bonus'];
            $data['type'] = $type;
            $data['action_user'] = $param['con_username'];
//            $data['remark'] = $param['record_bonus'];
            if (isset($param['orderid'])) {
                $Paylog = new Paylog();
                $data['oid'] = $Paylog->where(['orderid' => $param['orderid']])->value('id');
            }
            $data['cate'] = $this->getcate($param['reason']);
            $ActionLogs->writelog("osapi","clientlogs","rec_user_bonus","2:",$data);
            $CardUserBonus = new CardUserBonus();
            $res = $CardUserBonus->allowField(true)->save($data);
            $ActionLogs->writelog("osapi","clientlogs","rec_user_bonus","3:",$res);
        }catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
            $ActionLogs->writelog("osapi","clientlogs","rec_user_bonus","e:",$res);
        }
    }


    /* 增加余额记录
     * mid  top_mid card_id c_id us_id openid code orderid oid num mode type cate source remark
     *
     */
    public function rec_user_fee($param,$type){
        $ActionLogs=new ActionLogs();
        try {
            $ActionLogs->writelog("osapi","clientlogs","rec_user_bonus","1:",$param);
            $data = $param;
            $data['num'] = $param['balance'];
            $data['type'] = $type;
            $data['action_user'] = $param['con_username'];
//            $data['remark'] = $param['record_balance'];
            if (isset($param['orderid'])) {
                $Paylog = new Paylog();
                $data['oid'] = $Paylog->where(['orderid' => $param['orderid']])->value('id');
            }
            $data['cate'] = $this->getcate($param['reason']);
            $ActionLogs->writelog("osapi","clientlogs","rec_user_fee","2:",$data);;
            $CardUserFee = new CardUserFee();
            $res = $CardUserFee->allowField(true)->save($data);
            $ActionLogs->writelog("osapi","clientlogs","rec_user_bonus","3:",$res);
        }catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
            $ActionLogs->writelog("osapi","clientlogs","rec_user_fee","e:",$res);
        }
    }

    // 原始reason 转现在的 cate
    public function getcate($id){
        switch ($id){
            case "1":
                $r = '2';
                break;
            case "2":
                $r = '1';
                break;
            case "3":
                $r = '10';
                break;
            case "4":
                $r = '3';
                break;
            case "5":
                $r = '4';
                break;
            case "6":
                $r = '6';
                break;
            case "7":
                $r = '5';
                break;
            case "19":
                $r = '8';
                break;
            case "11":
                $r = '7';
                break;
            default :
                $r = '0';
                break;
        }
        return $r;
    }



}